package com.cqit.bluetoothkeybox.controller;

import com.cqit.bluetoothkeybox.common.Result;
import com.cqit.bluetoothkeybox.service.WechatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/wx/user")
@Api(tags = "微信用户接口")
@Validated
public class WxUserInfoController {

    @Autowired
    private WechatService wechatService;

    /**
     * 获取微信用户openId
     *
     * @param authCode 微信授权码
     * @return 微信用户openId
     */
    @GetMapping("/login")
    @ApiOperation(value = "获取微信用户openId", notes = "获取微信用户openId")
    @ApiImplicitParam(name = "authCode", value = "微信授权码")
    public Result<String> getOpenId(@NotBlank(message = "微信授权码不能为空") String authCode) throws Exception {
        return Result.success(wechatService.getOpenId(authCode));
    }

    @GetMapping("/getAccessToken")
    @ApiOperation(value = "获取微信用户AccessToken", notes = "获取微信用户AccessToken")
    public Result<String> getAccessToken() throws Exception {
        return Result.success(wechatService.getAccessToken());
    }

    @GetMapping("/getUnionid")
    @ApiOperation(value = "【用户支付完成后，获取该用户的 UnionId】获取微信用户Unionid",
            notes = "【用户支付完成后，获取该用户的 UnionId】获取微信用户Unionid")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "accessToken"),
            @ApiImplicitParam(name = "openId", value = "openId")
    })
    public Result<String> getUnionid(String accessToken, String openId) throws Exception {
        return Result.success(wechatService.getUnionid(accessToken, openId));
    }


}
